For this example we'll use a "top-down" approach. This means we'll create a Plain Old Java Object (POJO) with some annotations to indicate how we want JPA to persist it. We're letting the EntityManager take care of creating the tables in the database for us.
Create a Simple Class
The following class contains everything you need to begin persisting it to a database: Person.java
Note, for our configuration this step is optional.
If you use libraries provided exclusively by JBoss and Company, then you do not need to update your persistence.xml. If you are using another vendor or you want to make sure that your solution will work regardless of your persistence provider, add the following line to your persistence.xml:
Now we need to update our unit test class, Person.java. We will have it insert two people, query and verify that the people we created are in the database: PersonTest.java
packageentity;importstaticorg.junit.Assert.assertEquals;importstaticorg.junit.Assert.assertTrue;importjava.util.List;importjavax.persistence.EntityManager;importjavax.persistence.EntityManagerFactory;importjavax.persistence.Persistence;importorg.apache.log4j.BasicConfigurator;importorg.apache.log4j.Level;importorg.apache.log4j.Logger;importorg.junit.After;importorg.junit.Before;importorg.junit.Test;publicclass PersonTest {privatefinal Person p1 = new Person("Brett", 'L', "Schuchert", "Street1",
"Street2", "City", "State", "Zip");privatefinal Person p2 = new Person("FirstName", 'K', "LastName",
"Street1", "Street2", "City", "State", "Zip");private EntityManagerFactory emf;private EntityManager em;
@Before
publicvoid initEmfAndEm(){
BasicConfigurator.configure();Logger.getLogger("org").setLevel(Level.ERROR);
emf = Persistence.createEntityManagerFactory("examplePersistenceUnit");
em = emf.createEntityManager();}
@After
publicvoid cleanup(){
em.close();}
@SuppressWarnings("unchecked")
@Test
publicvoid insertAndRetrieve(){
em.getTransaction().begin();
em.persist(p1);
em.persist(p2);
em.getTransaction().commit();finalList<Person> list = em.createQuery("select p from Person p")
.getResultList();
assertEquals(2, list.size());for(Person current : list){finalString firstName = current.getFirstName();
assertTrue(firstName.equals("Brett")
|| firstName.equals("FirstName"));}}}
Re-run this test (the short-cut for this is Ctrl-Fll). Verify that everything is green.
Create a Simple Class
The following class contains everything you need to begin persisting it to a database:
Person.java
Update persistence.xml
Note, for our configuration this step is optional.If you use libraries provided exclusively by JBoss and Company, then you do not need to update your persistence.xml. If you are using another vendor or you want to make sure that your solution will work regardless of your persistence provider, add the following line to your persistence.xml:
Your updated persistence.xml is now:
Inserting and Querying
Now we need to update our unit test class, Person.java. We will have it insert two people, query and verify that the people we created are in the database:PersonTest.java
Re-run this test (the short-cut for this is Ctrl-Fll). Verify that everything is green.